package com.achievement.controller;

import com.achievement.domain.dto.honor.*;
import com.achievement.domain.param.honor.AchievementExpertParam;
import com.achievement.domain.param.honor.HonorApplyBookParam;
import com.achievement.domain.param.honor.HonorApplyInfoParam;
import com.achievement.domain.param.honor.ReviewPlanParam;
import com.achievement.domain.vo.honor.*;
import com.achievement.service.*;
import com.alibaba.fastjson2.JSONObject;
import com.common.annotation.Anonymous;
import com.common.annotation.Log;
import com.common.core.controller.BaseController;
import com.common.core.domain.AjaxResult;
import com.common.core.page.TableDataInfo;
import com.common.enums.BusinessType;
import com.common.utils.SecurityUtils;
import com.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "成果管理-获奖评审")
@RestController
@RequestMapping("/achievement/reviewPlan")
public class ReviewPlanController extends BaseController {

    @Autowired
    IHonorApplyBookService honorApplyBookService;

    @Autowired
    IExpertReviewService expertReviewService;
    @Autowired
    private IReviewPlanService ReviewPlanService;
    @Autowired
    private IAchievementExpertService expertService;

    @PreAuthorize("@ss.hasPermi('achievement:reviewPlan:list')")
    @ApiOperation("管理员获取评审计划列表")
    @GetMapping("/list")
    public TableDataInfo list(ReviewPlanParam param) {
        startPage();
        List<ReviewPlanBookExpertNumVO> list = ReviewPlanService.reviewPlanBookExpertNumByPage(param);
        return getDataTable(list);
    }
    /**
     * 左上方统计 全部 未启动 申报中 已停止
     * @param param 检索参数
     * @return AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('achievement:reviewPlan:list-statistics')")
    @ApiOperation("管理员-获取评审计划左上方分组")
    @GetMapping(value = "/list-statistics")
    public AjaxResult listStatistics( ReviewPlanParam param)
    {
        List<HashMap<String, Integer>> list =ReviewPlanService.listStatistics(param);
        return success(list);
    }

    /**
     * 获取评审计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('achievement:reviewPlan:query')")
    @ApiOperation("获取评审计划详情")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(ReviewPlanService.getInfoById(id));
    }

    /**
     * 新增评审计划
     */
    @PreAuthorize("@ss.hasPermi('achievement:reviewPlan:add')")
    @ApiOperation("新增评审计划")
    @Log(title = "新增评审计划", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody ReviewPlanDTO dto) {
        return toAjax(ReviewPlanService.add(dto));
    }

    /**
     * 修改评审计划
     */
    @PreAuthorize("@ss.hasPermi('achievement:reviewPlan:update')")
    @ApiOperation("修改评审计划")
    @Log(title = "修改评审计划", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/update")
    public AjaxResult edit(@RequestBody ReviewPlanDTO dto) {
        return toAjax(ReviewPlanService.update(dto));
    }
    /**
     * 获奖成果导出
     */
    @PreAuthorize("@ss.hasPermi('achievement:reviewPlan:exportHonorApplyBookList')")
    @ApiOperation("管理员-获奖评审列表导出")
    @Log(title = "获奖评审列表导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportHonorApplyBookList")
    public void exportHonorApplyBookList(HttpServletResponse response,@RequestBody Map<String,Object> requestMap)
    {
        HonorApplyBookParam param = JSONObject.parseObject(JSONObject.toJSONString(requestMap), HonorApplyBookParam.class);
        List<String> selectedColumns = (List<String>) requestMap.get("selectedColumns");
        List<HonorApplyBookExcelVO> list = honorApplyBookService.lisExcelByPage(param);
        ExcelUtil<HonorApplyBookExcelVO> util = new ExcelUtil<>(HonorApplyBookExcelVO.class);
        util.showColumn(selectedColumns.toArray(new String[0]));
        util.exportExcelByOrder(response, list, "获奖评审列表",selectedColumns.toArray(new String[0]));
    }
    /**
     * 更新评审计划状态
     */
    @PreAuthorize("@ss.hasPermi('achievement:reviewPlan:updateStatusById')")
    @ApiOperation("管理员-更新评审计划状态reviewPlanStatus 启动review  停止stop")
    @PutMapping(value = "/updateStatusById")
    public AjaxResult updateStatusById(@RequestBody ReviewPlanDTO dto) {
        return toAjax(ReviewPlanService.update(dto));
    }

    /**
     * 删除评审计划
     */
    @PreAuthorize("@ss.hasPermi('achievement:reviewPlan:remove')")
    @ApiOperation("管理员-删除评审计划")
    @Log(title = "删除评审计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/del/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids) {
        return toAjax(ReviewPlanService.batchRemove(ids));
    }

    /**
     * 获奖评审指派专家，获取所有专家信息
     */
    @PreAuthorize("@ss.hasPermi('achievement:reviewPlan:allExpertList')")
    @ApiOperation("管理员-获奖评审指派专家，获取所有专家信息")
    @GetMapping(value = "/allExpertList")
    public AjaxResult getAllExpertList() {
        return success(honorApplyBookService.allExpertList());
    }

    /**
     * 获奖评审指派专家，勾选专家保存
     */
    @PreAuthorize("@ss.hasPermi('achievement:reviewPlan:addReviewExpert')")
    @ApiOperation("管理员-获奖评审指派专家，勾选专家保存")
    @Log(title = "获奖评审指派专家，勾选专家保存", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addReviewExpert")
    public AjaxResult addReviewExpert(@RequestBody HonorApplyBookExpertReviewDTO dto) {
        return toAjax(expertReviewService.addOrUpdateExpertReview(dto));
    }
    @PreAuthorize("@ss.hasPermi('achievement:reviewPlan:honorApplyBookList')")
    @ApiOperation("管理员-获奖评审成果列表")
    @GetMapping("/honorApplyBookList")
    public TableDataInfo honorApplyBookList(HonorApplyBookParam param) {
        startPage();
        List<HonorApplyBookReviewVO> list = honorApplyBookService.listByPage(param);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('achievement:reviewPlan:honorApplyBookExpertReviewList')")
    @ApiOperation("管理员-获奖评审成果列表专家意见汇总")
    @GetMapping("/honorApplyBookExpertReviewList")
    public AjaxResult honorApplyBookExpertReviewList(@RequestParam String honorApplyBookId) {
        return success(honorApplyBookService.honorApplyBookExpertReviewDetailById(honorApplyBookId));
    }
    @PreAuthorize("@ss.hasPermi('achievement:reviewPlan:expertGroupList')")
    @ApiOperation("管理员-获取专家分组信息")
    @GetMapping("/expertGroupList")
    public AjaxResult expertGroupList(){
        return success(expertReviewService.expertGroupList());
    }
    @PreAuthorize("@ss.hasPermi('achievement:reviewPlan:expertListByGroup')")
    @ApiOperation("管理员-根据分组获取分组下专家列表")
    @GetMapping("/expertListByGroup")
    public AjaxResult expertListByGroup(@RequestParam String id){
        return success(expertReviewService.expertListByGroup(id));
    }
    @PreAuthorize("@ss.hasPermi('achievement:reviewPlan:expertListByReviewPlan')")
    @ApiOperation("管理员-评审计划-查看计划下专家")
    @GetMapping("/expertListByReviewPlan")
    public TableDataInfo expertListByGroup(AchievementExpertParam param){
        startPage();
        List<ExpertReviewByPlanVO> list = expertService.getPageByReviewPlanId(param);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('achievement:reviewPlan:expertReviewBookListByReviewPlan')")
    @ApiOperation("管理员-评审计划-查看计划下专家-点击专家获取专家评审成果")
    @GetMapping("/expertReviewBookListByReviewPlan")
    public TableDataInfo expertReviewBookListByReviewPlan(@Param("applyInfoId") String applyInfoId,@Param("reviewerId") String reviewerId) {
        startPage();
        List<HonorApplyBookReviewVO> list = honorApplyBookService.expertReviewBookListByReviewPlan(applyInfoId,reviewerId);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('achievement:reviewPlan:honorApplyBookListByExpert')")
    @ApiOperation("专家角色-成果评审-获取获奖评审列表 历史评审 0未评审  1已评审")
    @GetMapping("/honorApplyBookListByExpert")
    public AjaxResult honorApplyBookListByExpert(@Param("reviewStatus") Integer reviewStatus) {
        List<HonorApplyBookByExpertVO> list = honorApplyBookService.honorApplyBookListByExpert(reviewStatus,SecurityUtils.getUserRealId());
        return success(list);
    }
    @PreAuthorize("@ss.hasPermi('achievement:reviewPlan:honorApplyBookReviewResultByExpert')")
    @ApiOperation("专家角色-成果评审-查看评审")
    @GetMapping("/honorApplyBookReviewResultByExpert")
    public AjaxResult honorApplyBookReviewResultByExpert(@RequestParam String honorApplyBookId) {
        String expertId=SecurityUtils.getUserRealId();
        String expertSysId=SecurityUtils.getUserId().toString();
        return success(honorApplyBookService.honorApplyBookExpertReviewDetailByIdAndExpertId(honorApplyBookId,expertId,expertSysId));
    }
    @PreAuthorize("@ss.hasPermi('achievement:reviewPlan:refuseReviewByExpert')")
    @ApiOperation("专家角色-成果评审-拒绝评审")
    @PostMapping("/refuseReviewByExpert")
    public AjaxResult refuseReviewByExpert(@RequestBody ExpertReviewDTO dto) {
        String expertId=SecurityUtils.getUserRealId();
        return success(expertReviewService.refuseReviewByExpert(dto,expertId));
    }
    @PreAuthorize("@ss.hasPermi('achievement:reviewPlan:reviewByExpert')")
    @ApiOperation("专家角色-成果评审-提交评分 暂存")
    @PostMapping("/reviewByExpert")
    public AjaxResult reviewByExpert(@RequestBody ExpertReviewResultDTO dto) {
        String expertId=SecurityUtils.getUserRealId();
        boolean checkMark= expertReviewService.checkMark(dto.getReviewResultDTOs());
        if(checkMark){
            return success(expertReviewService.reviewByExpert(dto,expertId));
        }else {
            return error("评分不能为负数");
        }

    }
    @PreAuthorize("@ss.hasPermi('achievement:reviewPlan:reviewNoSubmitByExpert')")
    @ApiOperation("专家角色-成果评审-批量将已经保存的暂存提交 reviewObjectIds逗号分割")
    @PostMapping("/reviewNoSubmitByExpert")
    public AjaxResult reviewNoSubmitByExpert(@Param("reviewObjectIds") String reviewObjectIds) {
        String expertId=SecurityUtils.getUserRealId();
        return success(expertReviewService.reviewNoSubmitByExpert(reviewObjectIds,expertId));
    }
    @PreAuthorize("@ss.hasPermi('achievement:reviewPlan:approveHonor')")
    @ApiOperation("管理员-成果评审-批准获奖")
    @PostMapping("/approveHonor")
    public AjaxResult approveHonor(@RequestBody HonorApplyBookDTO dto) {
        return success(honorApplyBookService.approveHonor(dto));
    }
    @PreAuthorize("@ss.hasPermi('achievement:reviewPlan:revokeHonor')")
    @ApiOperation("管理员-成果评审-撤销获奖")
    @PostMapping("/revokeHonor")
    public AjaxResult revokeHonor(@RequestBody HonorApplyBookDTO dto) {
        return success(honorApplyBookService.revokeHonor(dto));
    }
    @PreAuthorize("@ss.hasPermi('achievement:reviewPlan:updateHonorState')")
    @ApiOperation("管理员-成果评审-更新成果获奖状态,建议通过，不建议通过，获奖")
    @PostMapping("/updateHonorState")
    public AjaxResult updateHonorState(@RequestBody HonorApplyBookDTO dto) {
        return success(honorApplyBookService.updateHonorState(dto));
    }

}
